package com.yixun.qfbao.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import com.yixun.qfbao.api.service.AreaService;
import com.yixun.qfbao.model.Area;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.common.GetAreaReqDto;
import com.yixun.qfbao.response.BaseResDto;
import com.yixun.qfbao.response.area.AreaConvert;
import com.yixun.qfbao.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.yixun.qfbao.annotations.EnumToMap;
import com.yixun.qfbao.annotations.EnumToList;
import com.yixun.qfbao.constants.ApiConstant;
import com.yixun.qfbao.response.BaseResponse;

import io.swagger.annotations.Api;


@Controller
@ResponseBody
@RequestMapping("/common")
@Api(tags = {"公共接口"})
public class CommonController {

    @Autowired
    private AreaService areaService;

    @EnumToMap
    @SuppressWarnings("unchecked")
    @GetMapping("/getEnum/{key}")
    public BaseResponse<Map<Object, String>> getEnum(HttpServletRequest request, @PathVariable("key") String key) {
        BaseResponse baseResponse = new BaseResponse();
        Map<Object, String> map = (Map<Object, String>)request.getAttribute(ApiConstant.ENUMMAP);
        baseResponse.setData(map);
        return baseResponse;
    }
    
    @EnumToList
    @SuppressWarnings("unchecked")
    @GetMapping("/getEnumNew/{key}")
    public BaseResponse<List<Map<Object, String>>> getEnumNew(HttpServletRequest request, @PathVariable("key") String key) {
        BaseResponse baseResponse = new BaseResponse();
        List<Map<String, Object>> returnList = (List<Map<String, Object>>)request.getAttribute(ApiConstant.ENUMMAP);
        baseResponse.setData(returnList);
        return baseResponse;
    }

    @PostMapping("/getCommonArea")
    public BaseResponse<List<BaseResDto>> getCommonArea(@RequestBody BaseRequest<GetAreaReqDto> request) {
        BaseResponse baseResponse = new BaseResponse();
        List<Area> areaList = areaService.getCommonArea(request.getData().getParentId(), request.getData().getName());
        List<BaseResDto> resDtoList = areaList.stream()
                .map(AreaConvert::convertArea2BaseDto)
                .collect(Collectors.toList());
        baseResponse.setData(resDtoList);
        return baseResponse;
    }


    /**
     * 根据redis key 清除redis
     * @param request
     * @return
     */
    @PostMapping("/delRedisByKey")
    public void delRedisByKey(String key) {
    	//1、获取公司对应渠道的APPID "APPID_" + companyUuid + "_" + systemId  
    	RedisUtil.delete(key);
    }
    
    /**
     * 根据redis key 获取redis
     * @param request
     * @return
     */
    @PostMapping("/getRedisByKey")
    public String getRedisByKey(String key) {
    	return String.valueOf(RedisUtil.get(key));
    }

}
